package cn.edu.nuc.onlinestore.vo;
import java.io.File;
import java.io.Serializable;
import java.util.Scanner;

import cn.edu.nuc.onlinestore.model.Admin;
import cn.edu.nuc.onlinestore.stream.ObjectStream;
public class AdminDo implements Serializable{

		/**
	 * 
	 */
	private static final long serialVersionUID = -7691968596136795401L;
		
		private String msg;
		private String adminname;
		private String adminword;
		public AdminDo(String adminbane,String adminword){
			File file=new File("D:/store/admin");
			if(!file.exists()){
				file.mkdir();
			}
			this.adminname=adminbane;
			this.adminword=adminword;
		}
		/**
		 * 注册用户，并将其写入文件
		 * @return
		 */
		public boolean adminAdd(){
			File file=new File("D:/store/admin/"+adminname+".txt");
			if(file.exists()){
				msg="用户名已存在，是否直接登录";
				return false;
			}else{
				ObjectStream.write("d:/store/admin/"+adminname+".txt",new Admin(adminname,adminword));
				msg="创建成功,您的用户名为:"+adminname+"密码为:"+adminword;
			}
			return false;
		}
		public String getMsg() {
			return msg;
		}
		public boolean adminUpdatepassword(String newpass){
			Admin admin=ObjectStream.read(Admin.class,"d:/store/admin/"+adminname+".txt");
			admin.setPassword(newpass);
			ObjectStream.write("admin/"+adminname+".txt",admin);
			msg="更改密码成功";
			return true;
		}
		/**
		 * 从文件中读取用户名密码,判断登录
		 * @return
		 */
		public boolean adminCheck(){
			System.out.println(adminname+adminword);
			File file=new File("D:/store/admin/"+adminname+".txt");
			if(file.exists()){
				//执行文件读取操作
				Admin admin=ObjectStream.read(Admin.class,"d:/store/admin/"+adminname+".txt");
				//System.out.println(adminword+admin.getPassword());
				if(adminword.equals(admin.getPassword())){
					msg="恭喜您,"+adminname+"登陆成功";
					return true;
				}else{
					msg="用户名密码错误";
					return false;
				}
			}else{
				//返回是否注册提示页面
					msg="该用户名不存在";
					return false;
			}
		}
		public static void main(String args[]){
			
			@SuppressWarnings("resource")
			Scanner in=new Scanner(System.in);
			System.out.println("请输入任命管理员的用户名密码，以回车隔开");
			AdminDo admindo=new AdminDo(in.next(),in.next());
			admindo.adminAdd();
			System.out.println(admindo.getMsg());
		}
}

